package com.heiyu.api.controller;

import com.google.common.collect.Lists;
import com.heiyu.api.response.BalanceInfoListResponse;
import com.heiyu.api.response.BalanceSeqListResponse;
import com.heiyu.api.response.model.BalanceInfoEntity;
import com.heiyu.api.response.model.BalanceInfoList;
import com.heiyu.api.response.model.BalanceSeqEntity;
import com.heiyu.api.response.model.BalanceSeqList;
import com.heiyu.constants.BalanceRefWebsite;
import com.heiyu.constants.ResponseMsgEnum;
import com.heiyu.controller.ApiBaseController;
import com.heiyu.dao.UserBalanceMapper;
import com.heiyu.dao.UserBalanceSeqMapper;
import com.heiyu.dao.UserBalanceTypeMapper;
import com.heiyu.entity.UserBalance;
import com.heiyu.entity.UserBalanceSeq;
import com.heiyu.entity.UserBalanceType;
import com.heiyu.entity.UserInfo;
import com.heiyu.utils.DateUtils;
import com.heiyu.utils.MoneyUtils;
import com.heiyu.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@Api(description = "账户信息")
@RestController
@RequestMapping("/api/v3/balance")
public class BalanceInfoController extends ApiBaseController {


    @Autowired
    private UserBalanceMapper userBalanceMapper;
    @Autowired
    private UserBalanceTypeMapper userBalanceTypeMapper;
    @Autowired
    private UserBalanceSeqMapper userBalanceSeqMapper;

    @ApiOperation(value = "list",notes = "账户列表")
    @GetMapping("list")
    public BalanceInfoListResponse list(){
        BalanceInfoListResponse response  = new BalanceInfoListResponse();
        response.setData(new BalanceInfoList());
        response.getData().setList(Lists.newArrayList());
        UserInfo userInfo = getUser();
        if(userInfo == null){
            response.error(ResponseMsgEnum.USER_NOT_LOGIN);
            return response;
        }
        UserBalance balance = new UserBalance();
        balance.setUserId(userInfo.getId());
        List<UserBalance> list = this.userBalanceMapper.selectList(balance);
        if(!CollectionUtils.isEmpty(list)){
            BalanceInfoEntity entity;
            UserBalanceType type;
            for(UserBalance userBalance:list){
                entity = new BalanceInfoEntity();
                type = this.userBalanceTypeMapper.selectPrimary(userBalance.getAccountType());
                entity.setBalanceId(userBalance.getId());
                entity.setName(type.getTypeName());
                entity.setAmunt(MoneyUtils.formatBalance(userBalance.getBalance()));
                response.getData().getList().add(entity);
            }
        }
        response.success();
        return response;
    }

    @ApiOperation(value = "seq/list",notes = "流水列表")
    @GetMapping("seq/list")
    public BalanceSeqListResponse seqList(@RequestParam Long balanceId,@RequestParam Integer pageIndex,@RequestParam Integer pageSize){
        BalanceSeqListResponse response = new BalanceSeqListResponse();
        response.setData(new BalanceSeqList());
        response.getData().setList(Lists.newArrayList());
        UserInfo userInfo = getUser();
        if(userInfo == null){
            response.error(ResponseMsgEnum.USER_NOT_LOGIN);
        }else{
            UserBalance userBalance = this.userBalanceMapper.selectPrimary(balanceId);
            if(userBalance.getUserId()!= userInfo.getId()){
                response.error("鉴权失败");
            }else{
                Map<String,Object> param = PageUtils.newParam(pageIndex,pageSize);
                param.put("balanceId",balanceId);
                List<UserBalanceSeq> list =  this.userBalanceSeqMapper.page(param);
                if(!CollectionUtils.isEmpty(list)){
                    BalanceSeqEntity entity;
                    for(UserBalanceSeq seq:list){
                        entity = new BalanceSeqEntity();
                        entity.setAmount(MoneyUtils.formatBalance(seq.getNum()));
                        entity.setShortDesc(seq.getShortDesc());
                        entity.setCreatedDate(DateUtils.format(seq.getCreatedTime(),"yyyy-MM-dd"));
                        entity.setWebsite(BalanceRefWebsite.getWebsite(seq.getActivityId()));
                        response.getData().getList().add(entity);
                    }
                }
                response.success();
            }
        }
        return response;
    }
}
